<?php

/*
 * Plugin Name: aitools
 * Plugin URI: -----
 * Description: This plugin expands the pagelines theme to add links to flag ai posts as good/bad and to trash posts from the loop 
 * Version: 1.0
 * Author: Mark Tilly
 * Author URL: http://www.memeposts.com
 * License: GPLv2 or later
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/


add_action('pagelines_loop_after_post_content','mct_ai_addtools');

function mct_ai_addtools() {
    global $post;
    $tag_ids = get_the_tags();
    if ($tag_ids == '')
        return ;
    foreach ($tag_ids as $tagname) {
        if ($tagname->name == 'from ai') {
            $post_dat = $post->post_content;
            #Get link, should only be one when from ai
            $regexp = '{<a [^>]*href\s*=\s*("|\')([^"\']*)("|\')[^>]*>}i';  
            $num_links = preg_match($regexp,$post_dat, $matches);
            if ($num_links == 0){
                $regexp = '{<a [^>]*href\s*=\s*(\\\\"|.\')([^"\']*)(\\\\"|.\')[^>]*>}i';  //try escaped href, used .\' because couldn't end up with \' 
                $num_links = preg_match($regexp,$post_dat, $matches);
            }
            #Set up trash link, even if no link (especially so...)
            echo '  <a class="post-edit-link" href="'.get_delete_post_link($post->ID).'" >[Trash]</a>';
            if ($num_links ==0) {
                break;
            }
            $urlstr = urlencode($matches[2]);
            $golink = "http://www.memeposts.com/ai/cgi-bin/train-ai.py?action=remote&urlstr=".$urlstr."&class=";
            echo '  <a class="post-edit-link" href="'.$golink.'good" >[good]</a>';
            echo '  <a class="post-edit-link" href="'.$golink.'bad" >[bad]</a>';
            break;
        }
    }

}
?>